package com.gla.Controller;

import com.gla.common.Const;
import com.gla.domain.Address;
import com.gla.domain.Shop;
import com.gla.domain.User;
import com.gla.result.Result;
import com.gla.service.IAddressService;
import com.gla.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@CrossOrigin
@RestController
public class AddressController {
    @Autowired
    IAddressService addressService;
    //添加收货地址
    @RequestMapping("/addAddress")
    public ServerResponse add(Address address){
        //UserVO userVO = (UserVO)session.getAttribute(Const.CURRENT_USER);//获取用户id
        return addressService.add(address,address.getUser_id());
    }

    //查询所有收货地址
    @GetMapping("/selectAddress")
    public Result getAddress(){
        List<Address> alladdress = addressService.getAddress();
        return Result.succeed(alladdress);
    }
    //根据id查询收货地址
    @GetMapping("/selectAddressById")
    public Result getAddressById(@RequestParam int user_id){
        List<Address> address = addressService.selectAddressById(user_id);
        return Result.succeed(address);
    }

    //修改收货地址
    @RequestMapping("/updateAddressById")
    public ServerResponse update(Address address){
        ServerResponse serverResponse = addressService.updateAddress(address);
        return serverResponse;
    }

    //删除收货地址
    @GetMapping("/deleteAddressById")
    public Result delete(@RequestParam Integer id){
        addressService.deleteAddressById(id);
        return Result.succeed("删除收货地址成功 id:"+id);
    }

    //查询默认地址
    @GetMapping("/selectDefaultAddById")
    public Result selectDefaultAddress(@RequestParam int user_id){
        List<Address> list = addressService.selectDefaultAddById(user_id);
        return Result.succeed(list.get(0));
    }
}
